package com.example.lenovo.downloaddemo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lenovo.downloaddemo.R;
import com.example.lenovo.downloaddemo.bean.HomeBean;
import com.example.lenovo.downloaddemo.http.AppConfig;
import com.example.lenovo.downloaddemo.http.okhttp.utils.AppContext;
import com.example.lenovo.downloaddemo.util.TextStyleLanguage;
import com.squareup.picasso.Picasso;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private List<HomeBean.DataBean.GoodsListBean> data;
    private OnItemClickLitener   mOnItemClickLitener;
    //设置回调接口
    public interface OnItemClickLitener{
        void onItemClick(View view, int position);
    }
    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener){
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    public RecyclerViewAdapter(List<HomeBean.DataBean.GoodsListBean> data){
       this.data = data;
    }


   static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView iv_recyler;
        TextView tv_recyler;


       public ViewHolder(@NonNull View itemView) {
           super(itemView);
           iv_recyler=(ImageView) itemView.findViewById(R.id.iv_recyler);
           tv_recyler=(TextView) itemView.findViewById(R.id.tv_recyler);
       }
   }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        if(!TextStyleLanguage.isEngLish()){
            holder.tv_recyler.setText(data.get(position).getGood_name());
        }else{
            holder.tv_recyler.setText(data.get(position).getGood_name_en());
        }



        Picasso.with(AppContext.getInstance())
                .load(AppConfig.IP+data.get(position).getGood_pic())
                .into(holder.iv_recyler);


        if(mOnItemClickLitener!=null){
            holder.iv_recyler.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickLitener.onItemClick(view,position);
                }
            });

        }
    }



    @Override
    public int getItemCount() {
        return data.size();
    }
}
